package com.xique.blelock.widget;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.xique.blelock.Interface.DialogOfDateListener;
import com.xique.blelock.R;
import com.xique.blelock.Utils.ScreenUtil;

import java.util.Date;

/**
 * Created by wxm on 2017/3/3.
 */

public class DialogOfDate implements View.OnClickListener {
    private Context context;
    private TextView tv_nowDate;// 选中的日期
    private CalendarView calendarview;

    public DialogOfDate(Context context) {
        this.context = context;
    }

    public void showDialog(View layout, final DialogOfDateListener dialogOfDateListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_date, null);
        tv_nowDate = (TextView) view.findViewById(R.id.date);
        calendarview = (CalendarView) view.findViewById(R.id.calendar);
        view.findViewById(R.id.month_left).setOnClickListener(this);
        view.findViewById(R.id.month_right).setOnClickListener(this);

        calendarview.setSelectMore(false);
        calendarview.setCalendarData(new Date());
        String[] ya = calendarview.getYearAndmonth().split("-");
        String m = ya[1];
        if (m.length()==1) {
            m = "0" + m;
        }
        tv_nowDate.setText(ya[0] + "年" + m + "月");

        final PopupWindow popupWindow = new PopupWindow();
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(context,R.drawable.bg_white));
        popupWindow.setContentView(view);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setHeight( ScreenUtil.getViewHeight(view));
        popupWindow.setWidth(ScreenUtil.getScreenWidth(context));
        popupWindow.showAsDropDown(layout);

        view.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogOfDateListener.onSelected(calendarview.getSelectedDate());
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.month_left:
                String leftmonth = calendarview.clickLeftMonth();
                String[] leftmouthary = leftmonth.split("-");
                String m = leftmouthary[1];
                if (m.length()==1) {
                    m = "0"+m;
                }
                tv_nowDate.setText(leftmouthary[0] + "年" + m + "月");
                break;

            case R.id.month_right:
                String rightmonth = calendarview.clickRightMonth();
                String[] rightmouthary = rightmonth.split("-");
                String m1 = rightmouthary[1];
                if (m1.length()==1) {
                    m1= "0"+m1;
                }
                tv_nowDate.setText(rightmouthary[0] + "年" + m1 + "月");
                break;
        }
    }
}
